<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0" />
    <link rel="stylesheet" type="text/css" href="__CSS__/lib/normalize.css" />
    <link rel="stylesheet" type="text/css" href="__CSS__/mui-3.6.1.min.css" />
    <link rel="stylesheet" type="text/css" href="__CSS__/my_addr.css" />
    <link rel="stylesheet" type="text/css" href="__CSS__/xiadan.css" />
    <link rel="stylesheet" type="text/css" href="__CSS__/activities.css" />
    <style type="text/css">
        a{ color: #000; }
    </style>
    <title>确认订单</title>
</head>

<body>
<script type="text/javascript" src="__JS__/jquery2.1.1.min.js"></script>
<script type="text/javascript" src="__JS__/xiadan.js"></script>
<script type="text/javascript" src="__JS__/layer.min.js"></script>
    <input type="hidden" id="uId" value="{$uId}" />
    <input type="hidden" id="sId" value="{$shopInfo['shopId']}" />
    <div class="mui-content">
        <div class="top-bar mui-row">

            <div class="return mui-col-sm-2 mui-col-xs-2" <if condition="$_GET['isSeckill']==1 or $_GET['isGroup']==1">onclick="history.back(-1)"<else />onclick="location.href='/Wx/Shop/shop?id={$shopInfo['shopId']}&ref=/Wx/Index/index'"</if>></div>
            <div class="top-middle mui-col-sm-8 mui-col-xs-8">
                <div class="s-top-middle">
                    确认订单
                </div>
            </div>
            <div class="top-right mui-col-sm-2 mui-col-xs-2"></div>
        </div>
    </div>
    <div class="xz">
        <if condition="$addrInfo">
            <ul id="addr">
                <volist name="addrInfo" id="ad">
                <li class="list existsAddr" id="address" data-id="{$ad.addressId}" >
                    <a href="/Wx/Address/myAddr?ref={$addressUrl}">
                        <div class="top mui-clearfix">
                            <div class="gps"></div>
                            <div class="name">
                                <span class="user_name">{$ad.userName}</span>
                                <span class="phone">{$ad.userPhone}</span>
                            </div>
                            <div class="dizhi">收货地址：{$ad.province}{$ad.city}{$ad.area}{$ad.address}</div>
                        </div>
                    </a>
                </li>
                </volist>
            </ul>
        <else/>
            <div data-id='' id="address" class="s-xz" onclick="location.href='/Wx/Address/myAddr?ref={$addressUrl}'"><img src="__IMG__/search/jia.png">选择或新增收货地址</div>
        </if>

         <!--  -->
    </div>

    <div>
        <div class="sjps">
            <div  class="ss-sjps">商家配送</div><span data-id="1" class="duigou duigou-active"></span>
        </div>
<!--         <div class="sjps">
            <div  class="ss-sjps">到店自取</div><span data-id="0" class="duigou"></span>
        </div> -->
    </div>

    <div class="ljsc">
        <div class="sdsj">送达时间</div><span class="s-ljsc">
            <select id="sTime" name="sTime">
                <foreach name="time[0][time_list]" item="v">
                    <option value="{$v}">
                        {$v}
                    </option>
                </foreach>
            </select><img src="__IMG__/search/8-3@2x.png" class="right-arrows"/></span>
    </div>

    <div style="clear:both"></div>

    <volist name="info['cartgoods']" id="cat">
        <php>
            $shopKey=$key;
        </php>

        <div class="ljsc" style="margin-top:0px">
            <div class="sdsj">店铺优惠</div><span class="s-ljsc">
                <input type="hidden" data-shopId="{$shopKey}" id="{$shopKey}_coupon"  data-money="0" class="coupon"  value="0" name="couponMoney"/>
                <select shopid="{$shopKey}">
                <option value="0" data-money="0" >满{$freeMoney|default=0}元包邮</option>
                <volist name="cat['couponList']" id="coupon">
                    <option value="{$coupon.youhui_id}" data-money="{$coupon.couponMoney}" >{$coupon.name}</option>
                </volist>
                </select><img src="__IMG__/search/8-3@2x.png" class="right-arrows"/></span>
        </div>



    <div class="dhz" onclick="location.href='/Wx/Shop/shop/id/{$shopInfo['shopId']}'">
        <div class="sdsj">{$shopInfo['shopName']}</div>
    </div>

    <foreach name="cat['shopgoods']" item="v">
        <div class="kyy">
            <div>{$v['goodsName']}<if condition="$v['isGroup']">【团】<elseif condition="$v['isSeckill']"/>【秒】<else/></if>
                {$v['attrName']}
            </div>
            <div class="x3">x{$v['cnt']}</div>
            <div class="y13">￥{$v['shopPrice']}</div>
        </div>
    </foreach>
    </div>


    <!--运费-->
        <input type="hidden" value="{$shopInfo.deliveryMoney}" id="deliveryMoney">
        <input type="hidden" value="{$shopInfo.deliveryMoney}" id="deliveryMoney2">
    </volist>


    <!--秒杀或团购倒计时 start-->
    <if condition="$daojishi">
    <div class="timediv" style="position: static !important;margin: 10px 0;padding: 0 12px">
        <span class="timespan"></span>
    </div>
    <script type="text/javascript" src="__JS__/activities.js"></script>
    <script>
        var starttime = new Date({$daojishi});//倒计时
    </script>
    <!--秒杀或团购倒计时 end-->
    </if>
    <input type="hidden" name="province" id="province" value="{$addrInfo[0].areaId1}"/>
    <input type="hidden" name="city" id="city" value="{$addrInfo[0].areaId2}"/>
    <input type="hidden" name="area" id="area" value="{$addrInfo[0].areaId3}"/>
    <input type="hidden" name="community" id="community" value="{$addrInfo[0].communityId}"/>

    <div style="height: 45px;margin-bottom: 20px;"></div>
    <div class="b-zong">
        <div class="zongj">
            <span class="ss-zj">共<font style="color: #ff4045">{$info.totalNum}</font>件
                (运费<span id="postageMoney" class="postage"></span>)总计:&nbsp;&nbsp;</span>
            <span id="totalMoney" data-money="{$info.totalMoney|default=0}">{$info.totalMoney|default=0}</span>元
        </div>
        <div class="xyb" onclick="submitOrder()">下一步</div>
    </div>
<script type="text/javascript" src="__JS__/jquery2.1.1.min.js"></script>
<script type="text/javascript" src="__JS__/xiadan.js"></script>
<script type="text/javascript" src="__JS__/layer.min.js"></script>


<script type="text/javascript">
    $(function(){
        postage();
    })
    //选择门店自提还是商家配送
    $('.sjps').click(function(){
        var check = $('.duigou-active').data('id');
        if(check==1){
            $('#deliveryMoney').val(0);
        }else{
            var deliveryMoney2 = $('#deliveryMoney2').val();
            $('#deliveryMoney').val(deliveryMoney2);
        }
        postage();
    });



    //更新运费和总价格
    function postage(){
        var deliveryMoney = $('#deliveryMoney').val();//隐藏域实际运费
        $('#postageMoney').text(deliveryMoney);//结算显示的运费

        //总价格 = 店铺商品总价+邮费-优惠券
        var lastMoney   = 0;
        var totalMoney  =$('#totalMoney').attr('data-money');//商品总金额
        var postageMoney=$('#postageMoney').text();//总邮费
        var couponMoney =$('input[name="couponMoney"]').attr('data-money');//总优惠价
        lastMoney = parseFloat(totalMoney)+parseFloat(postageMoney)-parseFloat(couponMoney);
        //console.log(totalMoney+','+postageMoney+','+couponMoney+','+lastMoney);
        $('#postageMoney').text(postageMoney+"元");
        $('#totalMoney').text(lastMoney);

    }





$('select').change(function(){
    var shopid=$(this).attr('shopid');
    var money=$(this).find("option:selected").attr('data-money');
    var couponId=$(this).find("option:selected").val();
   // console.log(shopid);
   // console.log(money);
   // console.log(couponId);
    $('#'+shopid+'_coupon').val(couponId);
    $('#'+shopid+'_coupon').attr('data-money',money);
    postage();
})



$(".selt").change(function(){
    var shopid=$(this).attr('data-shopId');
    var val=$('#'+shopid+'-isSelf').val();
    if(val==0){
        $('#'+shopid+'-isSelf').val(1);
        $('#ziqi_'+shopid).show();
        $('#kuandi_'+shopid).hide();
        $('#postage_'+shopid).text(0.0);
        postage();
    }else{
        var free=$('#'+shopid+'-freight').val();
        $('#'+shopid+'-isSelf').val(0);
        $('#ziqi_'+shopid).hide();
        $('#kuandi_'+shopid).show();
        $('#postage_'+shopid).text(free);
        postage();
    }

});

//运费选择处理
function postage2(){
    var totalPostage=0;//总邮费
    var shopPostage=0;//店铺的邮费
    var totalMoney=0;//全部金额
    var couponTotalMoney=0;//总优惠价
    $('.postage').each(function(){
        console.log(this);
        //店铺ID
        var shopid=$(this).attr('data-shopid');
        //邮费
        var shopPosatage=Number($(this).text());
        //店铺优惠价
        var couponShopMoney=0;
        var coupon_money=$('#'+shopid+'_coupon').attr('data-money');
        if(!isNaN(coupon_money)){
            couponShopMoney=coupon_money;
        }
        //couponShopMoney=Number($('#'+shopid+'_coupon').attr('data-money'));

        //店铺价格
        var shopMoney=Number($('#'+shopid+'-shopMoney').val());
        shopMoney=shopMoney-couponShopMoney;
        if(shopMoney<=0){
            shopMoney=0;
        }
        //店铺总价
        var shopTotalMoney=shopPosatage+shopMoney;
        //总优惠价
        couponTotalMoney+=couponShopMoney;

        //总邮费
        totalPostage+=shopPosatage;
        //所有总价
        totalMoney+=shopTotalMoney;
        $('#'+shopid+'_totalMoney').text(shopTotalMoney);
    })
    $('#postageMoney').text(totalPostage);

    $('#totalMoney').text(totalMoney);
}

function isself(_this){
    var shopid=$(_this).attr('data-shopId');
    localStorage.shopid=shopid;
    var val=$('#'+shopid+'-isSelf').val();
    if($('#kuandi_'+shopid).is(':visible')) {
        $('#'+shopid+'-isSelf').val(0);
        $('.ps').attr("checked",true);
        $('.zq').attr("checked",false);
    } else {
        $('#'+shopid+'-isSelf').val(1);
        $('.ps').attr("checked",false);
        $('.zq').attr("checked",true);
    }
    var html=$('#selectPS').html();
    layer.open({
          type: 1,
          title:'选择配送方式',
          skin: 'layui-layer-rim', //加上边框
          area: ['80%', '30%'], //宽高
          content:html
        });
}

//提交订单
function submitOrder(){

    var province=$('#province').val();
    var city=$('#city').val();
    var area=$('#area').val();
    var community=$('#community').val();
    var shopTotalCheckAll = $(".shopTotalCheck");
    var tempValue,tempShopName;
    for (var g=0;g<shopTotalCheckAll.length;g++){

        tempValue = $(shopTotalCheckAll[g]).attr('data-shopminpay');

        if(Number(tempValue)==0){
            tempShopName = $(shopTotalCheckAll[g]).attr('data-shopName');


            layer.msg(tempShopName+'店铺订单未达到配送价');
            return;
        }

    };


    // if(!province||typeof(province)=='undefined'){

    //     layer.msg('请选择地址');
    //     return;
    // }
    // if(!city||typeof(city)=='undefined'){

    //     layer.msg('请选择地址');
    //     return;
    // }
    // if(!area||typeof(area)=='undefined'){

    //     layer.msg('请选择地址');
    //     return;
    // }
    layer.msg('正在保存数据...', {icon: 16});
    var consigneeId = $('#address').data('id');
    if(!consigneeId||typeof(consigneeId)=='undefined'){
        layer.msg('请选择地址');
        return;
    }
    var url="{:U('Wx/Confirm/checkGoodsStock')}"
        jQuery.post(url ,{consigneeId:consigneeId},function(data) {
            console.log(data);
          if(data.status==-5){
              layer.msg('请填写收货地址');
              return;
          }else if(data.status==-6){
              // var ww=window.screen.availWidth-2;
              // var arr=data['data'].split(',');
              // for(var i=0; i<arr.length;i++){
              //     $('.goods_'+arr[i]).css('width',ww+'px');
              //     $('.goods_'+arr[i]).css('border','#ff4045 dashed 1px');
              //     $('.goods_'+arr[i]).css('background','#D5D7DC');
              // }
              layer.msg('该收货地址不在配送范围');
              return;
          }
        var flag = true;
        var strSale='商品';
        var ww=window.screen.availWidth-2;
        $.each(data, function(key, value) {
            if(value['isSale']==0){
                //下架或库存不足
                flag=false;
                $('.goods_'+key).css('width',ww+'px');
                 $('.goods_'+key).css('background','#D5D7DC');
                 $('.goods_'+key).css('border','#ff4045 dashed 1px');
                strSale+=value['goodsName']+',';
            }
        });
        strSale=strSale.substring(0,strSale.length-1);
        strSale+='商品下架或库存不足';
        if(flag){
            if(!consigneeId||consigneeId==''||typeof(consigneeId)=='undefined'){
                layer.msg('请选择地址');
                return;
            }
            var payway = $('input:radio[name="payway"]:checked').val();
            var isself = $('#isself').val();
            var addressId=$('#selectAddrId').val();

            //留言
            var remark='';
            $('.remark').each(function(){
                var shopid=$(this).attr('data-shopid');
                var val=$(this).val();
                if(val){
                    remark+=shopid+'|o_o|'+val+'|o2o|';
                }
            })
            //留言结束

            //配送方式
            var isSlef='';
//          $('.isSelf').each(function(){
//              var shopid=$(this).attr('data-shopId');
//              var val=$(this).val();
//              if(val){
//                  isSlef+=shopid+'|o_o|'+val+'|o2o|';
//              }
//          })
            // $(".isself").each(function(){
            //     var shopid=$(this).attr('data-shopId');
            //     var val=$(this).val();
            //     if(val){
            //         isSlef+=shopid+'|o_o|'+val+'|o2o|';
            //     }
            // });


            //优惠卷
            var coupon='';
            $('.coupon').each(function(){
                var shopid=$(this).attr('data-shopId');
                var val=$(this).val();
                if(val){
                    coupon+=shopid+'|o_o|'+val+'|o2o|';
                }
            });
            var orderunique = new Date().getTime();
            var requireTime = $('#sTime').val();
            var isGroup = 0;
            <?php if($_GET['isGroup'] == 1){ ?>
                isGroup = 1;
            <?php } ?>
           $.ajax({
               type: "POST",
               url: "{:U('Wx/Orders/submitOrder')}",
               data: {isGroup:isGroup,coupon:coupon,payway:payway,consigneeId:consigneeId,isself:$('.duigou-active').data('id'),remarks:remark,orderunique:orderunique,addressId:addressId,requireTime:requireTime},
               dataType: "json",
               success: function(data){
                    //layer.closeAll();
                    if(data.status==-1||data.status==-2||data.status==-3){
                        layer.msg(data.msg);
                        return;
                    }else if(data.status==1){
                        layer.msg(data.msg);
                        if(data.payid){
                            //存在订单号直接进入付款页
                            location.href="/Wx/Confirm/onlinkPay/payid/"+data.payid+"";
                        }else{
                            //跳转到订单列表页
                            location.href="{:U('Wx/Orders/orders')}";
                        }
                    }
                }
           });
        }else{
            layer.msg(strSale);
            //alert(strSale);
        }
    });
}






</script>
</body>

</html>
